<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Kelas extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('flexigrid');
        $this->load->library('flexigrid');
		$this->load->library('session');
		$this->load->library('form_validation');
        $this->load->model('kelas_model');
        $this->load->model('ustad_model');
        $this->load->model('periode_model');
        $this->load->model('kelas_santri_model');
    }

//end Main
    //load Grid
    function index($id_periode) {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
			$this->session->set_userdata('id_periode', $id_periode);
            $colModel['no'] = array('No', 30, TRUE, 'center', 0);
            $colModel['Nama_Kelas'] = array('Kelas', 200, TRUE, 'center', 1);
            $colModel['hari'] = array('Hari', 100, TRUE, 'center', 1);
            $colModel['jam'] = array('Jam', 100, TRUE, 'center', 1);
            $colModel['ruang_kelas'] = array('Ruang Kelas', 180, TRUE, 'center', 1);
            $colModel['nama_ustad'] = array('Ustad', 180, TRUE, 'center', 1);
            $colModel['Edit'] = array('Edit', 30, TRUE, 'center', 0);
			$colModel['Lihat_Data_Santri'] = array('Lihat Data Santri', 100, TRUE, 'center', 0);
			$colModel['Generate Absensi'] = array('Cetak Absensi', 100, TRUE, 'center', 0);
			
			//get_periode_name
			$periode = $this->periode_model->selectone($id_periode);
			foreach ($periode->result() as $row) {
                $nama_periode = $row->NAMA_PERIODE;
                $mulai = $row->MULAI;
                $selesai = $row->SELESAI;
            }
			$this->session->set_userdata('nama_periode', $nama_periode);
			$this->session->set_userdata('mulai', $mulai);
			$this->session->set_userdata('selesai', $selesai);


            $gridParams = array(
                'width' => 'auto',
                'height' => 350,
                'rp' => 10,
                'rpOptions' => '[5,10,15,20,25,40]',
                'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
                'blockOpacity' => 0.5,
                'title' => 'Daftar Kelas</br>Periode : '.$nama_periode,
                'showTableToggleBtn' => true
            );

            $buttons[] = array('Tambah', 'add', 'spt_js');
            $buttons[] = array('separator');
            $buttons[] = array('Hapus', 'delete', 'spt_js');
			$buttons[] = array('separator');
            $buttons[] = array('Kembali ke daftar periode', 'back', 'spt_js');


            // mengambil data dari file controler ajax pada method grid_berkas
            $grid_js = build_grid_js('flex1', site_url("kelas/grid_kelas/".$id_periode.""), $colModel, 'Nama_Kelas', 'asc', $gridParams, $buttons);

            $data['added_js'] =
                    "<script type='text/javascript'>
		function spt_js(com,grid)
		{
			if (com=='Tambah')
			{
				location.href='" . base_url() . "index.php/kelas/form';
			}
			
			if (com=='Kembali ke daftar periode')
			{
				location.href='" . base_url() . "index.php/periode';
			}

			if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' kelas?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" . base_url() . "index.php/kelas/delete" . "',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
							   }
							});
						}
					} else {
						return false;
					}
				}

		}
		</script>
		";


            $data['js_grid'] = $grid_js;

            //rendering view
            //$data['title'] = 'Daftar Peserta';
            //$data['js_grid']=$grid_js;
            $data['content'] = $this->load->view('grid', $data, true);
            $this->load->view('main', $data);
        }
    }

    function grid_kelas($id_periode) {
        $valid_fields = array('Nama_Kelas', 'Jadwal_Kelas', 'nama_ustad', 'hari', 'jam', 'ruang_kelas');
        $this->flexigrid->validate_post('Nama_Kelas', 'asc', $valid_fields);
        $records = $this->kelas_model->get_data_kelas($id_periode);
        $this->output->set_header($this->config->item('json_header'));

        $no = 0;
        foreach ($records['records']->result() as $row) {
		$kelas = str_replace("\'", "'", $row->NAMA_KELAS);
            $record_items[] = array(
                $row->ID_KELAS,
                $no = $no + 1,
                //$row->NAMA_KELAS,
				$kelas,
                $row->hari,
                $row->jam,
                $row->ruang_kelas,
                $row->nama_ustad,
                '<a href=\'' . base_url() . 'index.php/kelas/edit/' . $row->ID_KELAS . '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/edit.png\'></a> ',
                '<a href=\'' . base_url() . 'index.php/kelas_santri/index/' . $row->ID_KELAS . '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/folder.png\'></a> ',
                '<a target="_blank" href=\'' . base_url() . 'index.php/kelas_santri/generate_absensi/' . $row->ID_KELAS . '\'><img border=\'0\' src=\'' . base_url() . 'public/images/print.png\'></a> '
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }

    function form() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $data['status'] = 'new';
            $data['failed'] = false;
            $data['aa'] = '';
			$data['ustad'] = $this->ustad_model->get_data_all_ustad();
            $data['content'] = $this->load->view('form_kelas', $data, true);
            $this->load->view('main', $data);
        }
    }

    function add($id_periode) {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
			if($this->cek_validasi() == false)
			{
				$this->form();
			}
			else{
				$data = array(
					'NAMA_KELAS' => $this->input->post('nama_kelas'),
					'hari' => $this->input->post('hari'),
					'jam' => $this->input->post('jam'),
					'ruang_kelas' => $this->input->post('ruang_kelas'),
					'ID_USTAD' => $this->input->post('id_ustad'),
					'ID_PERIODE' => $id_periode
				);
				$data = str_replace("'", "\'", $data);
				$this->kelas_model->insert($data);
				redirect('kelas/index/'.$id_periode.'');
			}
        }
    }

    public function edit($id) {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $data['status'] = 'edit';
            $record_artikel = $this->kelas_model->selectone($id);
            foreach ($record_artikel->result() as $artikel) {
                $data['id_kelas'] = $artikel->ID_KELAS;
                $data['nama_kelas'] = $artikel->NAMA_KELAS;
                $data['hari'] = $artikel->hari;
                $data['jam'] = $artikel->jam;
                $data['ruang_kelas'] = $artikel->ruang_kelas;
                $data['id_ustad'] = $artikel->ID_USTAD;
            }
            //$data['status'] = 'new';
            $data['failed'] = false;
            $data['aa'] = '';
			$data = str_replace("\'", "'", $data);
			$data['ustad'] = $this->ustad_model->get_data_all_ustad();
            $data['content'] = $this->load->view('form_kelas', $data, true);
            $this->load->view('main', $data);
        }
    }

    function update($id) {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
			if($this->cek_validasi() == false)
				{
					$this->edit($id);
				}
				else{
					$data = array(
						'NAMA_KELAS' => $this->input->post('nama_kelas'),
						'hari' => $this->input->post('hari'),
						'jam' => $this->input->post('jam'),
						'ruang_kelas' => $this->input->post('ruang_kelas'),
						'ID_USTAD' => $this->input->post('id_ustad')
					);
					$data = str_replace("'", "\'", $data);
					$this->kelas_model->update($id, $data);
					redirect('kelas/index/'.$this->session->userdata('id_periode').'');
				}
        }
    }

    
    function delete() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $spt_ids_post_array = explode(",", $this->input->post('items'));
            foreach ($spt_ids_post_array as $index => $id) {
                if (isset($id) && $id != '') {
                    $this->kelas_model->delete($id);
                    $this->kelas_santri_model->delete_by_kelas($id);
                }//end if
            }//end foreach
        }
    }
	
	
	function cek_validasi() {
        // Setting Rules
        $this->form_validation->set_rules('nama_kelas', 'Nama Kelas', 'required');
        $this->form_validation->set_rules('hari', 'Hari', 'required');
        $this->form_validation->set_rules('jam', 'Jam', 'required');
        //$this->form_validation->set_rules('tgl_selesai', 'Tanggal Selesai', 'required');
		
		
        //Setting Error Message
        $this->form_validation->set_message('required', 'Field %s harus diisi.');
        $this->form_validation->set_message('numeric', 'Field %s harus diisi angka.');
		$this->form_validation->set_message('greater_than', 'Field %s harus diisi angka lebih dari 0');


        // Setting Delimiter
        $this->form_validation->set_error_delimiters('<li class="error">', '</li>');
        return $this->form_validation->run();
    }

}

//end class

/* End of file welcome.php */
/* Location: ./system/application/controllers/main.php */
